﻿using EYangFrame.DataAccess.Global;
using EYangFramewirk.Business.MessageSystem.Interfaces;
using EYangFramework.Common.Ninject;
using EYangFramework.Domain.Entitys.Entrys;
using EYangFramework.Domain.Entitys.ValueObjects;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EYangFramework.MessageSystem
{
    public partial class frm_EditLinkUser : EYangFramework.CommonForm.frm_BasicForm
    {
        List<User> allUsers;
        List<LinkUser> allLinkUsers;
        bool isAdd;
        public frm_EditLinkUser()
        {
            this.AllowSingtonTimer = false;
            InitializeComponent();
        }

        private void frm_EditLinkUser_Load(object sender, EventArgs e)
        {
            SetTabVisible(tp_Detail, false);
            BindList();
            if (listLinkName.Items.Count <= 0)
            {
                BindAllUser();
            }
        }
        private void BindAllUser()
        {
            listAllUser.Items.Clear();
            using (IBLLLinkUser bLLLinkUser = NinjectProvider.NinjectCore.GetService<IBLLLinkUser>())
            {
                allUsers = bLLLinkUser.GetAllUsers().OrderBy(u => u.USERNAME).ToList();
                foreach (var item in allUsers)
                {
                    listAllUser.Items.Add(item.USERNAME);
                }
            }
        }

        private void BindList()
        {
            listLinkName.Items.Clear();
            using (IBLLLinkUser bLLLinkUser = NinjectProvider.NinjectCore.GetService<IBLLLinkUser>())
            {
                allLinkUsers = bLLLinkUser.GetLinkUsers(GlobalInfo.CurrentUser.ID).OrderBy(t => t.CREATETIME).ToList();
                foreach (var item in allLinkUsers)
                {
                    listLinkName.Items.Add(item.Name);
                }
            }
        }

        private void listLinkName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listLinkName.SelectedItem == null)
            {
                return;
            }
            string linkUserName = listLinkName.SelectedItem.ToString();
            LinkUser currentLinkUser = allLinkUsers.Where(l => l.Name == linkUserName).FirstOrDefault();
            if (currentLinkUser == null)
            {
                MessageBox.Show("数据查询错误,请联系管理员!", "警告!");
                return;
            }
            txtLinkName.Text = currentLinkUser.Name;
            if (allUsers == null)
            {
                using (IBLLLinkUser bLLLinkUser = NinjectProvider.NinjectCore.GetService<IBLLLinkUser>())
                {
                    allUsers = bLLLinkUser.GetAllUsers();
                }
            }
            //绑定所有用户
            listAllUser.Items.Clear();
            string[] linkedUsers = currentLinkUser.LinkUsers.Split(',');
            foreach (var item in allUsers.Where(u => !linkedUsers.Contains(u.USERNAME)).OrderBy(u=>u.USERNAME))
            {
                listAllUser.Items.Add(item.USERNAME);
            }
            //绑定已链接的用户
            listSelectedUser.Items.Clear();
            foreach (var item in linkedUsers)
            {
                listSelectedUser.Items.Add(item);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listLinkName.SelectedItem == null)
            {
                MessageBox.Show("请选择需要删除的项目!", "提示!");
                return;
            }
            if (MessageBox.Show("确定要删除选中的项目吗?", "警告!", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                using (IBLLLinkUser bLLLinkUser = NinjectProvider.NinjectCore.GetService<IBLLLinkUser>())
                {
                    bool isSuccess = bLLLinkUser.DeleteLinkUser(allLinkUsers.Where(l => l.Name == listLinkName.SelectedItem.ToString()).First().ID);
                    if (isSuccess)
                    {
                        MessageBox.Show("删除成功!");
                        BindList();
                    }
                }
            }
        }

        private void btnSelectedAll_Click(object sender, EventArgs e)
        {
            foreach (var item in listAllUser.Items)
            {
                if (!listSelectedUser.Items.Contains(item.ToString()))
                {
                    listSelectedUser.Items.Add(item);
                }
            }
            listAllUser.Items.Clear();
        }

        private void btnSelectSingle_Click(object sender, EventArgs e)
        {
            if (listAllUser.SelectedItem == null)
            {
                return;
            }
            string user = listAllUser.SelectedItem.ToString();
            if (!listSelectedUser.Items.Contains(user))
            {
                listSelectedUser.Items.Add(user);
            }
            listAllUser.Items.Remove(user);
        }

        private void btnRemoveSingle_Click(object sender, EventArgs e)
        {
            if (listSelectedUser.SelectedItem == null)
            {
                return;
            }
            string user = listSelectedUser.SelectedItem.ToString();
            if (!listAllUser.Items.Contains(user))
            {
                listAllUser.Items.Add(user);
            }
            listSelectedUser.Items.Remove(user);
        }

        private void btnRemoveAll_Click(object sender, EventArgs e)
        {
            foreach (var item in listSelectedUser.Items)
            {
                if (!listAllUser.Items.Contains(item.ToString()))
                {
                    listAllUser.Items.Add(item);
                }
            }
            listSelectedUser.Items.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtLinkName.Text))
            {
                MessageBox.Show("常用联系人名称不能为空!", "警告!");
                return;
            }
            if (listSelectedUser.Items.Count <= 0)
            {
                MessageBox.Show("常用联系人选择的用户不能为空!", "警告!");
                return;
            }
            LinkUser linkUser;
            bool isSuccess = false;
            if (allLinkUsers.Where(l => l.Name == txtLinkName.Text.Trim()).Count() <= 0)
            {
                linkUser = new LinkUser();
                linkUser.CREATEUSER = GlobalInfo.CurrentUser.USERNAME;
                linkUser.LinkUsers = GetSelectedUsers();
                linkUser.User = GlobalInfo.CurrentUser.ID.ToString();
                linkUser.Name = txtLinkName.Text.Trim();
                using (IBLLLinkUser bLLLinkUser = NinjectProvider.NinjectCore.GetService<IBLLLinkUser>())
                {
                    isSuccess = bLLLinkUser.InsertLinkUser(linkUser);
                }
            }
            else
            {
                using (IBLLLinkUser bLLLinkUser = NinjectProvider.NinjectCore.GetService<IBLLLinkUser>())
                {
                    linkUser = bLLLinkUser.GetCurrentLinkUser(allLinkUsers.Where(l => l.Name == txtLinkName.Text.Trim()).First().ID);
                    linkUser.LinkUsers = GetSelectedUsers();
                    isSuccess = bLLLinkUser.UpdateLinkUser(linkUser);
                }
            }
            if (isSuccess)
            {
                MessageBox.Show("保存成功!");
                BindList();
            }
        }

        private string GetSelectedUsers()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in listSelectedUser.Items)
            {
                sb.Append(item + ",");
            }
            return sb.ToString().Trim(',');
        }
    }
}
